{% template "admin/header.html" %}
<div class="admin-main layui-anim layui-anim-upbit" >
    <fieldset class="layui-elem-field layui-field-title">
        <legend>添加</legend>
    </fieldset>
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">父级</label>
            <div class="layui-input-4">
                <select name="pid" lay-verify="required" lay-filter="pid" id="auths">
                    <option value="0">默认顶级</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-4">
                <input type="text" name="title" lay-verify="required" placeholder="分类名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-4">
                <input type="text" name="sort" value="10" placeholder="从小到大排序" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">提交</button>
                <a href="JavaScript:window.history.back();" class="layui-btn layui-btn-primary">返回</a>
            </div>
        </div>
    </form>
</div>

{% template "admin/footer.html" %}
<script src="/static/common/js/angular.min.js"></script>
<script>
    var m = angular.module('hd',[]);
    let _token = '';
    let pid = getUrlParam("pid");

    layui.use(['form', 'layer'], function () {
        var form = layui.form, $ = layui.jquery;
        form.on('submit(submit)', function (data) {
            // 提交到方法 默认为本身
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post("/admin/upload_file_type/add", data.field, function (res) {
                layer.close(loading);
                if (res.Code > 0) {
                    layer.msg(res.Msg, {time: 1800, icon: 1}, function () {
                        window.history.back();
                    });
                } else {
                    layer.msg(res.Msg, {time: 1800, icon: 2});
                }
            });
        });

        function hanMenu(data,pid,level){
            var arr = [];
            var tem = null;
            for (var i=0; i<data.length; i++){
                if(data[i].Pid == pid){
                    tem = data[i];
                    tem['lvl'] = level + 1;
                    tem['lefthtml'] = "|— ".repeat(level);
                    tem['ltitle'] = tem['lefthtml'] + tem.Title;
                    arr.push(tem);
                    arr = arr.concat(hanMenu(data,data[i].Id,tem['lvl']));
                }
            }
            return arr;
        }

        $.post('/admin/upload_file_type/query',function (data) {
            data = hanMenu(data.Data,0,0);
            $.each(data,function (k,v) {
                $('#auths').append('<option value="'+v.Id+'" '+(v.Id==pid?'selected':'')+'>'+v.ltitle+'</option>');
            });
            form.render();
        });
    });
</script>
</body>
</html>
